// SEE END OF FILE FOR LICENSE TERMS

#include "CANTelemetryManager.h"
#include "CANDataIDRefs.h"

// NEVER USE CONTROLS IN TELEMETRY - Controls are supported by specific data protocol
// Controls are - Radio inputs, Servo outputs, Autopilot outputs

/****************************************************************************
CANTelemetrySetup_x

Setup of telemetry data to transmit to other nodes.
This setup is specific to a node (x)

A number of lists of telemetry identifiers is supported.  
Each list can be transmitted at different rates.

Lists are in the form:
const TELEMETRY_LIST_ENTRY ListName[] =
	{
		{ID_xxx,0},
		{ID_yyy,0},
		{ID_yyy,n},
		{ID_NULL, 0}
	}

IMPORTANT: Every list must be terminated with the {ID_NULL, 0} entry

xxx,yyy = identifiers for data to be sent.
Identifiers are defined in CANDataIDRefs.h

Telemetry supports both normal values and array values.
The translation between identifiers and the real values is performed in
 CANTelemtryParser - For common UDB data
 CANTelemetryParser_x _ For variables specific to CAN interface

CANbus can only send up to 8 bytes in one go so large array values have to
bu split into many chunks.  Many identifier entries have to be entered to
transmit a larger array, for example:
			{ID_RMAT,0},
			{ID_RMAT,3},
			{ID_RMAT,6},
The size of data transmited and array/normal access is controlled by 
CANTelemetryParser.

In {ID_yyy,n}, n is the index into array data to be sent.
For non array data, n is ignored.

CAUTION: Transmitting an array value with maximum index above the data
size will have unpredicatable results.

A special identifier is used to send events or actions
	{ID_EVENT,ID_event}

Events are defined in CANDataIDRefs.h.
When an event is received, it is parsed by CANEventHandlers.c
If you intend to use event, carefully read the notes on using them


ptelemetryLists is a list of pointers to the identifier lists.
The Telemetry maanger uses ptelemetryLists to access the lists

The number of lists is defined by TELEMETRY_LIST_COUNT

A transmit request flag is maintained for each of the lists
When the list transmit request is asserted, the list is transmitted
A list item automatically has its transmit request flagged when 
its counter expires.  
The counter timeouts are set with telemetryAutoRequestCycles.
The requests are typically called every servo cycle (40Hz)

The top item of the list has priority over the others.  If its flag is
asserted whiue transmitting a lower priority list, the next transmit
item is chosen from the higher priority list.
Transmission of the lower priority entry is not immediately interrupted.


CAUTION: With the existing implementation, care must be taken to only transfer
a set of control data in one direction.  
  ie. both ends should not transfer servo outputs. The result will be unpredicatable


****************************************************************************/

// List of items to send at servo rate
const TELEMETRY_LIST_ENTRY DCMRateList[] = 
	{
			{ID_RMAT,0},
			{ID_RMAT,3},
			{ID_RMAT,6},
			{ID_IMU_LOCATIONX,0},
			{ID_IMU_LOCATIONY,0},
			{ID_IMU_LOCATIONZ,0},
			{ID_IMU_VELOCITYX,0},
			{ID_IMU_VELOCITYY,0},
			{ID_IMU_VELOCITYZ,0},

			{ID_EVENT,ID_AUTOPILOT_DCM_TELEMETRY_COMPLETE},
			{ID_NULL, 0}
	};

// List of items to send at 10Hz navigation rate
const TELEMETRY_LIST_ENTRY NavRateList[] = 
	{
			{ID_GPS_LOCATION,0},
			{ID_LOC_ERROR_EARTH,0},
			{ID_ESTIMATED_WIND,0},

			{ID_GPS_TOW,0},
			{ID_GPS_VARS,0},	// MODE1,MODE2,SVS,NAV_VALID
			{ID_GPS_VARS2,0},	// SOG, COG, CLIMB_GPS, WEEK_NO

			{ID_DESIRED_BEHAVOIR,0},
			{ID_ORIENTATION,0},

			{ID_MAG_FIELD_EARTH,0},
			{ID_AIRSPEED_MAG,0},
			{ID_EVENT,ID_AUTOPILOT_NAV_TELEMETRY_COMPLETE},
			{ID_NULL, 0}
	};


// List of items to send at 1Hz background rate
const TELEMETRY_LIST_ENTRY BackgroundRateList[] = 
	{
			{ID_ORIGIN_LAT, 0},
			{ID_ORIGIN_LONG, 0},
			{ID_ORIGIN_ALT, 0},
			{ID_EVENT,ID_AUTOPILOT_BACKGROUND_TELEMETRY_COMPLETE},
			{ID_NULL, 0}
	};


// List of telemetry lists
const TELEMETRY_LIST_ENTRY* ptelemetryLists[TELEMETRY_LIST_COUNT] = 
	{
			DCMRateList,
			NavRateList,
			BackgroundRateList
	};



// A list of the number of cycles requried to trigger a telemetry send
// One value is required per entry in the telemetry list
const unsigned int telemetryAutoRequestCycles[TELEMETRY_LIST_COUNT] = 
	{
		1,
		5,
		40
	};



/****************************************************************************/
// This is part of the servo and radio interface software
//
// ServoInterface source code
//	http://code.google.com/p/rc-servo-interface
//
// Copyright 2010 ServoInterface Team
// See the AUTHORS.TXT file for a list of authors of ServoInterface.
//
// ServoInterface is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ServoInterface is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License 
// along with ServoInterface.  If not, see <http://www.gnu.org/licenses/>.
//
// Many parts of ServoInterface use either modified or unmodified code
// from the MatrixPilot pilot project.
// The project also contains code for modifying MatrixPilot to operate
// with ServoInterface.
// For details, credits and licenses of MatrixPilot see the AUTHORS.TXT file.
// or see this website: http://code.google.com/p/gentlenav
/****************************************************************************/
